package cbfg.popup

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.*
import android.widget.PopupWindow
import androidx.annotation.FloatRange
import androidx.annotation.LayoutRes
import androidx.annotation.StyleRes
import androidx.core.graphics.ColorUtils

/**
 * @author:  Tom Hawk
 * 添加时间: 2020/9/7 16:45
 * 功能描述:
 */
class PopupWindowBuilder(private val context: Context, @LayoutRes private val layoutRes: Int) {
    private var width = ViewGroup.LayoutParams.WRAP_CONTENT
    private var height = ViewGroup.LayoutParams.WRAP_CONTENT
    private var isCancelable = true
    private var dimAmount = 0F

    /**
     * true：返回键关闭 PopupWindow 而不是直接退出页面
     */
    private var keyBackEnabled = true

    @StyleRes
    private var animStyle = 0

    private var layoutListener: LayoutListener? = null
    private var dismissListener: PopupWindow.OnDismissListener? = null

    fun resize(width: Int, height: Int): PopupWindowBuilder {
        this.width = width
        this.height = height
        return this
    }

    fun setCancelable(cancelable: Boolean): PopupWindowBuilder {
        this.isCancelable = cancelable
        return this
    }

    fun setKeyBackEnabled(enabled: Boolean): PopupWindowBuilder {
        this.keyBackEnabled = enabled
        return this
    }

    fun setAnimStyle(@StyleRes animStyle: Int): PopupWindowBuilder {
        this.animStyle = animStyle
        return this
    }

    fun useDefaultAnimStyle(gravity: Int): PopupWindowBuilder {
        this.animStyle = when (gravity) {
            Gravity.CENTER -> R.style.Popup_CenterAnim
            Gravity.BOTTOM -> R.style.Popup_BottomAnim
            Gravity.LEFT,
            Gravity.START -> R.style.Popup_LeftAnim
            Gravity.RIGHT,
            Gravity.END -> R.style.Popup_RightAnim
            else -> 0
        }
        return this
    }

    /**
     * 宽、高为 MATCH_PARENT 的时候有效
     */
    fun setDimAmount(@FloatRange(from = 0.0, to = 1.0) dimAmount: Float): PopupWindowBuilder {
        this.dimAmount = dimAmount
        return this
    }

    fun setLayoutListener(listener: LayoutListener?): PopupWindowBuilder {
        this.layoutListener = listener
        return this
    }

    fun setDismissListener(dismissListener: PopupWindow.OnDismissListener?): PopupWindowBuilder {
        this.dismissListener = dismissListener
        return this
    }

    fun create(): PopupWindow {
        val popupWindow = PopupWindow(width, height)
        popupWindow.animationStyle = animStyle
        popupWindow.isOutsideTouchable = isCancelable
        popupWindow.setOnDismissListener(dismissListener)
        /**
         * 点击空白区域PopupWindow消失，这里必须先设置setBackgroundDrawable，否则点击无反应
         */
        popupWindow.setBackgroundDrawable(ColorDrawable(0x00000000))
        /**
         * 解决被软件盘遮盖的问题 //WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE or
         */
        popupWindow.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
        popupWindow.inputMethodMode = PopupWindow.INPUT_METHOD_NEEDED
        /**
         * fix PopupWindow width、height 为 WRAP_CONTENT 时显示的效果可能不一致
         */
        popupWindow.contentView = DFrameLayout(context).also {
            it.layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            it.setOnTouchOutsideListener {
                if (isCancelable) {
                    popupWindow.dismiss()
                }
            }
            if (dimAmount != 0F
                && width == ViewGroup.LayoutParams.MATCH_PARENT
                && height == ViewGroup.LayoutParams.MATCH_PARENT
            ) {
                val bgColor = ColorUtils.setAlphaComponent(Color.BLACK, (dimAmount * 255).toInt())
                it.setBackgroundColor(bgColor)
            }
            LayoutInflater.from(context).inflate(layoutRes, it, true)
            layoutListener?.onViewCreated(it)
        }
        /**
         * 返回键关闭 PopupWindow 而不是直接退出页面
         */
        if (keyBackEnabled) {
            popupWindow.isFocusable = true
            popupWindow.contentView.isFocusable = true
            popupWindow.contentView.isFocusableInTouchMode = true
        }

        return popupWindow
    }

    interface LayoutListener {
        fun onViewCreated(view: View)
    }
}